<template>
  <el-dialog
    :title="!dataForm.id ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form class="verify-form" :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
    <el-form-item label="素材类型" prop="messageType">
      <el-radio-group v-model="dataForm.messageType">
        <el-radio :label="1">文本信息</el-radio>
        <el-radio :label="2">单图文信息</el-radio>
        <!-- <el-radio :label="3">多图文信息</el-radio> -->
      </el-radio-group>
    </el-form-item>
    <el-form-item label="素材主题" prop="messageTheme">
      <el-input v-model="dataForm.messageTheme" placeholder="素材主题"></el-input>
    </el-form-item>
    <el-form-item label="标题" prop="messageTitle">
      <el-input v-model="dataForm.messageTitle" placeholder="标题"></el-input>
    </el-form-item>
        <el-form-item label="消息描述" prop="messageDescription" v-if="dataForm.messageType ==2">
      <el-input v-model="dataForm.messageDescription" placeholder="消息描述"></el-input>
    </el-form-item>
    <el-form-item label="图片id" prop="pictureId" v-if="dataForm.messageType ==2">
      <el-input v-model="dataForm.pictureId" placeholder="图片id"></el-input>
    </el-form-item>
    <el-form-item label="链接类型" prop="linkType" v-if="dataForm.messageType ==2">
      <el-radio-group v-model="dataForm.linkType">
        <el-radio :label="1">直接跳转至外部网址活系统应用</el-radio>
        <el-radio :label="2">直接跳转至内部文章</el-radio>
      </el-radio-group>
    </el-form-item>
    <el-form-item label="链接地址" prop="linkUrl" v-if="dataForm.messageType ==2 && dataForm.linkType == 1">
      <el-input v-model="dataForm.linkUrl" placeholder="链接地址"></el-input>
    </el-form-item>
    <el-form-item label="内部文章id" prop="articleId" v-if="dataForm.messageType ==2 && dataForm.linkType == 2">
      <el-input v-model="dataForm.articleId" placeholder="内部文章id"></el-input>
    </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
    import {getWxMaterialObj, addWxMaterialObj, putWxMaterialObj} from '@/api/mp/wxmaterial'

    export default {
    data () {
      return {
        visible: false,
        dataForm: {
          id: 0,
          messageTheme: '',
          messageType: 1,
          messageTitle: '',
          messageDescription: '',
          pictureId: '',
          linkType: 1,
          linkUrl: '',
          articleId: '',
          createTime: '',
          createUser: '',
          updateTime: '',
          updateUser: '',
          delFlag: '',
          tenantId: ''
        },
        dataRule: {
          messageTheme: [
            { required: true, message: '素材(消息)主题不能为空', trigger: 'blur' }
          ],
          messageType: [
            { required: true, message: '素材(消息)类型(1-文本信息,2-单图文信息,3-多图文信息)不能为空', trigger: 'blur' }
          ],
          messageTitle: [
            { required: true, message: '标题(内容)不能为空', trigger: 'blur' }
          ],
          messageDescription: [
            { required: true, message: '消息描述不能为空', trigger: 'blur' }
          ],
          pictureId: [
            { required: true, message: '图片id不能为空', trigger: 'blur' }
          ],
          linkType: [
            { required: true, message: '链接类型(1-直接跳转至外部网址活系统应用，2-直接跳转至内部文章)不能为空', trigger: 'blur' }
          ],
          linkUrl: [
            { required: true, message: '链接地址不能为空', trigger: 'blur' }
          ],
          articleId: [
            { required: true, message: '内部文章id不能为空', trigger: 'blur' }
          ],
          createTime: [
            { required: true, message: '创建时间不能为空', trigger: 'blur' }
          ],
          createUser: [
            { required: true, message: '创建人不能为空', trigger: 'blur' }
          ],
          updateTime: [
            { required: true, message: '更新时间不能为空', trigger: 'blur' }
          ],
          updateUser: [
            { required: true, message: '修改人不能为空', trigger: 'blur' }
          ],
          delFlag: [
            { required: true, message: '删除标识(1-未删除,-1已删除)不能为空', trigger: 'blur' }
          ],
          tenantId: [
            { required: true, message: '租户Id不能为空', trigger: 'blur' }
          ]
        }
      }
    },
    methods: {
      init (id) {
        this.dataForm.id = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.id) {
            getWxMaterialObj(this.dataForm.id).then(response => {
                this.dataForm = response.data.data
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            if (this.dataForm.id) {
                putWxMaterialObj(this.dataForm).then(data => {
                    this.$message.success('修改成功')
                    this.visible = false
                    this.$emit('refreshDataList')
                });
            } else {
                addWxMaterialObj(this.dataForm).then(data => {
                    this.$message.success('添加成功')
                    this.visible = false
                    this.$emit('refreshDataList')
                })
            }
          }
        })
      }
    }
  }
</script>
